import csv
import cv2
import os


"""
从csv文件中读取到关键点然后绘制到图片上后保存到一个文件夹中
csv_file:要读取的csv文件
image_folder:图片的外部绝对路径
output_folder:绘制后的关键点文件夹
keypoints_indices_x:要绘制的关键点x下标
keypoints_indices_y:要绘制的关键点y下标
这个暂时只供eth使用
"""


def draw_keypoints_from_csv(csv_file, image_folder, output_folder, keypoints_indices_x, keypoints_indices_y):
    with open(csv_file, 'r') as file:
        reader = csv.reader(file)
        # headers = next(reader)
        iii = 0
        for row in reader:
            image_path = row[0]
            image_abs_path = os.path.join(image_folder, image_path)
            image = cv2.imread(image_abs_path)

            # 绘制关键点
            for i in range(0, len(keypoints_indices_x)):
                x_index = keypoints_indices_x[i]
                y_index = keypoints_indices_y[i]
                x = float(row[x_index])
                y = float(row[y_index])
                cv2.circle(image, (int(x), int(y)), 1, (0, 255, 0), 1)

            landmark_folder = os.path.join(output_folder, os.path.dirname(image_path))
            if not os.path.exists(landmark_folder):
                os.makedirs(landmark_folder)

            print(iii)
            iii += 1
            output_image_path = os.path.join(landmark_folder, os.path.basename(image_path))
            cv2.imwrite(output_image_path, image)


if __name__ == "__main__":
    print("1111")
    # # eth的关键点show
    # csv_file = r'F:\mzs_code\gaze_estimation\Dataset\eth\label\cleaned_few_eth.csv'
    # image_folder = r'F:\mzs_code\gaze_estimation\Dataset\eth\Image\train'
    # output_folder = r'F:\mzs_code\gaze_estimation\Dataset\eth\showlandmark'
    # num = 7
    # keypoints_indices = [468, 473, 130, 243, 463, 359, 27, 23, 257, 253]
    # keypoints_indices_x = [index * 2 + num for index in keypoints_indices]
    # keypoints_indices_y = [index * 2 + num + 1 for index in keypoints_indices]
    # draw_keypoints_from_csv(csv_file, image_folder, output_folder, keypoints_indices_x, keypoints_indices_y)
    # eyediap的关键点show
    csv_file = '/home/xian/mzs/mzs_code/Dataset/eyediap_2025/all_noselect_diap_2025.csv'
    image_folder = '/home/xian/mzs/mzs_code/Dataset/eyediap_2025/Image'
    output_folder = '/home/xian/mzs/mzs_code/Dataset/eyediap_2025/showlandmark'
    num = 11
    keypoints_indices = [468, 473, 130, 243, 463, 359, 27, 23, 257, 253]
    keypoints_indices_x = [index * 2 + num for index in keypoints_indices]
    keypoints_indices_y = [index * 2 + num + 1 for index in keypoints_indices]
    draw_keypoints_from_csv(csv_file, image_folder, output_folder, keypoints_indices_x, keypoints_indices_y)
